package com.lanqiao.gxnushop.controller;

import com.lanqiao.gxnushop.entity.CartItems;
import com.lanqiao.gxnushop.service.CartItemsService;
import com.lanqiao.gxnushop.valueObject.CarProduct;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@Log4j
@Tag(name = "CartItemController",description="购物车商品管理")
@RestController
public class CartItemsController {

    @Autowired
    private CartItemsService cartItemsService;

    @PostMapping("/cartItems")
    @Operation(summary="增加购物车商品")
    @Parameter(name = "cartItems",description = "购物车商品:cartItemId(自动生成不为null),buyerId(买家Id不为null),productId(商品Id不为null),quantity")
    public boolean addCartItems(CartItems cartItems) {

        System.out.println(cartItems);
        return cartItemsService.add(cartItems);
    }

    @DeleteMapping("/cartItems/{cartItemId}")
    @Operation(summary="删除购物车商品")
    @Parameter(name = "cartItemId",description = "购物车商品id:cartItemId(不为null)")
    public boolean deleteCartItems(@PathVariable String cartItemId) {
        return cartItemsService.delete(cartItemId);
    }

    @GetMapping("/cartItems/{buyerId}")
    @Operation(summary="根据买家buyerId查询购物车商品")
    @Parameter(name = "buyerId",description = "买家id:buyerId(不为null)")
    public ArrayList<CarProduct> findByBuyerId(@PathVariable String buyerId) {
        return cartItemsService.findByBuyerId(buyerId);
    }

    @PutMapping("/cartItems")
    @Operation(summary="根据购物车cartItemId更新购物车商品")
    @Parameter(name = "cartItems",description = "购物车商品cartItemId(不为null)只能更新数量")
    public boolean updateCartItems(CartItems cartItems) {
        return cartItemsService.update(cartItems);
    }

}
